
package fr.inria.arles.tempmanagement.preferencesstore.service;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;


public class QueryPortImpl implements QueryPort {
public String rfidQuery    
( String rfid ,  String partition )
throws  Exception_Exception  {

        String methodName = "RFIDQuery";
	SoapObject request = new SoapObject(Constants.NAMESPACE, methodName);
        

        PropertyInfo rfidPropInfo = new PropertyInfo();
	rfidPropInfo.setName("rfid");
	rfidPropInfo.setValue(rfid);
	rfidPropInfo.setType(PropertyInfo.STRING_CLASS);
	request.addProperty(rfidPropInfo);

        PropertyInfo partitionPropInfo = new PropertyInfo();
	partitionPropInfo.setName("partition");
	partitionPropInfo.setValue(partition);
	partitionPropInfo.setType(PropertyInfo.STRING_CLASS);
	request.addProperty(partitionPropInfo);

        return execute(request, methodName).toString();    
    }

public String authenticateQuery    
( String username ,  String password ,  String partition )
throws  Exception_Exception  {

        String methodName = "AuthenticateQuery";
	SoapObject request = new SoapObject(Constants.NAMESPACE, methodName);
        

        PropertyInfo usernamePropInfo = new PropertyInfo();
	usernamePropInfo.setName("username");
	usernamePropInfo.setValue(username);
	usernamePropInfo.setType(PropertyInfo.STRING_CLASS);
	request.addProperty(usernamePropInfo);

        PropertyInfo passwordPropInfo = new PropertyInfo();
	passwordPropInfo.setName("password");
	passwordPropInfo.setValue(password);
	passwordPropInfo.setType(PropertyInfo.STRING_CLASS);
	request.addProperty(passwordPropInfo);

        PropertyInfo partitionPropInfo = new PropertyInfo();
	partitionPropInfo.setName("partition");
	partitionPropInfo.setValue(partition);
	partitionPropInfo.setType(PropertyInfo.STRING_CLASS);
	request.addProperty(partitionPropInfo);

        return execute(request, methodName).toString();    
    }

public int getTempQuery    
( String id )
throws  Exception_Exception  {

        String methodName = "GetTempQuery";
	SoapObject request = new SoapObject(Constants.NAMESPACE, methodName);
        

        PropertyInfo idPropInfo = new PropertyInfo();
	idPropInfo.setName("ID");
	idPropInfo.setValue(id);
	idPropInfo.setType(PropertyInfo.STRING_CLASS);
	request.addProperty(idPropInfo);

        return Integer.parseInt(execute(request, methodName).toString());    
    }

public String updateTempQuery    
( String id ,  int temp )
throws  Exception_Exception  {

        String methodName = "UpdateTempQuery";
	SoapObject request = new SoapObject(Constants.NAMESPACE, methodName);
        

        PropertyInfo idPropInfo = new PropertyInfo();
	idPropInfo.setName("ID");
	idPropInfo.setValue(id);
	idPropInfo.setType(PropertyInfo.STRING_CLASS);
	request.addProperty(idPropInfo);

        PropertyInfo tempPropInfo = new PropertyInfo();
	tempPropInfo.setName("temp");
	tempPropInfo.setValue(temp);
	tempPropInfo.setType(PropertyInfo.INTEGER_CLASS);
	request.addProperty(tempPropInfo);

        return execute(request, methodName).toString();    
    }



    private Object execute(SoapObject request,String methodName) throws Exception_Exception {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
        
	envelope.setOutputSoapObject(request);
	AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				Constants.URL);

	try {
            androidHttpTransport.call(Constants.NAMESPACE + methodName, envelope);
            return envelope.getResponse();
	} catch (SoapFault e) {
            Exception ex=new Exception();
            ex.setMessage(e.getMessage());
            throw new Exception_Exception("SoapFault",ex,e);
	} catch (java.lang.Exception e) {
            return null;
	}

    }

}
